package cn.echoedu.command;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import cn.echoedu.command.base.ExecuteParam;
import cn.echoedu.command.base.FailureParam;
import cn.echoedu.command.base.SuccessParam;
import cn.echoedu.entity.TelContacts;
import cn.echoedu.util.L;

import com.google.gson.Gson;

public class HotContactsListCommand extends BaseCommand{

   private final String T="HotContactsListCommand--------->";


	public void executeSuccess(SuccessParam successparam) {
		// TODO Auto-generated method stub
		List<TelContacts> telcontacts=(List<TelContacts>)successparam.getParam();
		List<HotContactsEntity> listcontact=new ArrayList<HotContactsEntity>();
		HotContactsListSuccessResponse hotcontactslistsuccess=new HotContactsListSuccessResponse();
		for(TelContacts telcontact:telcontacts){
			HotContactsEntity contactentity=new HotContactsEntity();
			contactentity.setContact_name(telcontact.getTelcontactsname());
			 L.p(T+"name:"+telcontact.getTelcontactsname());
			contactentity.setContact_number(telcontact.getTel());
			contactentity.setContact_photo_url(telcontact.getImage().getImagesrc());

			listcontact.add(contactentity);
		}
		hotcontactslistsuccess.setList(listcontact);
		Gson gson=new Gson();
		String sendjson=gson.toJson(hotcontactslistsuccess);
		 send(sendjson,successparam.getResponse());
	}

	public void executeFailure(FailureParam failureparam) {
		// TODO Auto-generated method stub
		HotContactsListFailureResponse hotcontactslist=new HotContactsListFailureResponse();
        Gson gson=new Gson();
        String jsonstr=gson.toJson(hotcontactslist);
        String sendjson="";
        try {
    		sendjson=java.net.URLEncoder.encode(jsonstr,"utf-8");
    	} catch (UnsupportedEncodingException e1) {
    		// TODO Auto-generated catch block
    		e1.printStackTrace();
    	}
//    	   java.net.URLDecoder.decode(s)
    		PrintWriter pw;
    		try {
    			failureparam.getResponse().setCharacterEncoding("utf-8");
    			pw = failureparam.getResponse().getWriter();
    		    pw.print(sendjson);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
	}

	public void execute(ExecuteParam executeparam) {
		// TODO Auto-generated method stub
		
	}
	public class HotContactsEntity {

		private String contact_name="";
		private String contact_number="";
		private String contact_photo_url="";
		public String getContact_name() {
			return contact_name;
		}
		public void setContact_name(String contact_name) {
			this.contact_name = contact_name;
		}

		public String getContact_photo_url() {
			return contact_photo_url;
		}
		public void setContact_photo_url(String contact_photo_url) {
			this.contact_photo_url = contact_photo_url;
		}
		public String getContact_number() {
			return contact_number;
		}
		public void setContact_number(String contact_number) {
			this.contact_number = contact_number;
		}

	}
	public class HotContactsListFailureResponse {
		private int code=-305;
		private String msg="Hot Contacts List failure";
		public int getCode() {
			return code;
		}
		public void setCode(int code) {
			this.code = code;
		}
		public String getMsg() {
			return msg;
		}
		public void setMsg(String msg) {
			this.msg = msg;
		}

	}
	public class HotContactsListSuccessResponse {
		private int code=305;
		private String msg="Hot Success";
		private List<HotContactsEntity> list;
		public int getCode() {
			return code;
		}
		public void setCode(int code) {
			this.code = code;
		}
		public String getMsg() {
			return msg;
		}
		public void setMsg(String msg) {
			this.msg = msg;
		}
		public List<HotContactsEntity> getList() {
			return list;
		}
		public void setList(List<HotContactsEntity> list) {
			this.list = list;
		}
	 
	}
}
